package com.twu.biblioteca;

import java.io.IOException;
import java.util.ArrayList;

public class Library {

    private static Library library = new Library();

    private ArrayList<Book> BookList;
    private ArrayList<Movie> MovieList;
    private ArrayList<Customer> CustomerList;
    private Customer loggedInUser;

    private Console console;

    private Library() {

        BookList = new ArrayList<Book>();
        MovieList = new ArrayList<Movie>();
        CustomerList = new ArrayList<Customer>();

        console = new Console();
    }

    public static Library getLibraryInstance() {
        return library;
    }

    public void addBookToLibrary(Book book) {
        BookList.add(book);
    }

    public void addMovieToLibrary(Movie movie) {
        MovieList.add(movie);
    }

    public void addCustomerToLibrary(Customer customer) {
        CustomerList.add(customer);
    }

    public Book searchBook(String requestedBook) {

        Book book;
        for (int index = 0; index < BookList.size(); index++) {
            book = BookList.get(index);

            if(requestedBook.equals(book.getName()))
                return book;
        }
        return null;
    }

    public Movie searchMovie(String requestedMovie) {

        Movie movie;
        for (int index = 0; index < MovieList.size(); index++) {
            movie = MovieList.get(index);

            if(requestedMovie.equals(movie.getName()))
                return movie;
        }
        return null;
    }

    public void listBooks() {
        Book book;
        for (int index = 0; index < BookList.size(); index++) {
            book = BookList.get(index);

            if(book.isAvailable())
                console.print(console.formatString(book.toString()));
        }
    }

    public void listMovies() {
        Movie movie;
        for (int index = 0; index < MovieList.size(); index++) {
            movie = MovieList.get(index);

            if(movie.isAvailable())
                console.print(console.formatString(movie.toString()));
        }
    }


    public void checkoutBook() throws IOException {
        Book book;
        String requestedBook;

        console.print("Enter Book Name : ");
        requestedBook = console.getStringInputFromUser();
        book = library.searchBook(requestedBook);

        if(book != null && book.checkoutIfAvailable(loggedInUser))
            console.print("Enjoy the Book!");
        else
            console.print("The Book is not available");
    }


    public void checkoutMovie() throws IOException {
        Movie movie;
        String requestedMovie;

        console.print("Enter Movie Name : ");
        requestedMovie = console.getStringInputFromUser();
        movie = library.searchMovie(requestedMovie);

        if(movie != null)
            console.print("Enjoy the Movie!");
        else
            console.print("The Movie is not available");
    }

    public void returnBook() throws IOException {
        Book book;
        String requestedBook;

        console.print("Enter Book Name : ");
        requestedBook = console.getStringInputFromUser();
        book = library.searchBook(requestedBook);

        if(book != null && book.returnIfValid())
            console.print("Thank you for returning the book");
        else
            console.print("This is not a valid book to return");

    }

    public boolean authenticateUserLogin() throws IOException {
        String libraryNumber, password;

        console.print("---Enter Credentials---");

        console.print("LIBRARY NUMBER : ");
        libraryNumber = console.getStringInputFromUser();

        console.print("PASSWORD :");
        password = console.getStringInputFromUser();

        for(Customer customer : CustomerList) {
            if(customer.validateCredentials(libraryNumber, password)) {
                setLoggedInUser(customer);
                return true;
            }
        }
        return false;
    }

    private void setLoggedInUser(Customer customer) {
        this.loggedInUser = customer;
    }

    public void getUserInfo() {
        console.print(loggedInUser.toString());
    }
}